﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AssetManagementSystem.Model;
using AssetManagementSystem.Presenter;
using AssetManagementSystem.Utils;
using AssetManagementSystem.Utils.Events;
using AssetManagementSystem.View.IView;

namespace AssetManagementSystem.View.Controls {
    public partial class UIHoldEditor : Form, IStudentView, IAssetView, IHoldView {

        Dictionary<string, Asset> assetList;
        Dictionary<string, Student> studentList;
        List<Hold> holdList;

        //==================================================
        // IStudentView Events
        //==================================================
        public event EventHandler Students_LoadAll;
        public event EventHandler<SearchEventArgs> Students_Find;
        public event EventHandler<StudentEventArgs> Student_Create;
        public event EventHandler<StudentEventArgs> Student_Update;
        public event EventHandler<StudentEventArgs> Student_Delete;
        //==================================================

        //==================================================
        // IAssetView Events
        //==================================================
        public event EventHandler Assets_LoadAll;
        public event EventHandler<SearchEventArgs> Assets_Find;
        public event EventHandler<AssetEventArgs> Asset_Create;
        public event EventHandler<AssetEventArgs> Asset_Update;
        public event EventHandler<AssetEventArgs> Asset_Delete;
        public event EventHandler<AssetPropertiesEventArgs> AssetProperty_Delete;
        //==================================================

        //==================================================
        // IHoldView Events
        //==================================================
        public event EventHandler Holds_LoadAll;
        public event EventHandler<SearchEventArgs> Holds_Find;
        public event EventHandler<HoldEventArgs> Hold_Create;
        public event EventHandler<HoldEventArgs> Hold_Delete;
        //==================================================

        //==================================================
        // Shared Interface Methods
        //==================================================
        public void ShowMessage(string message) {
            MessageBox.Show(message);
        }
        //==================================================

        public UIHoldEditor() {
            InitializeComponent();

            assetList = new Dictionary<string, Asset>();
            studentList = new Dictionary<string, Student>();

            //Create a presenter to handle events and update display data
            AssetPresenter assetPresenter = new AssetPresenter(this);
            //Create a student presenter
            StudentPresenter studentPresenter = new StudentPresenter(this);
            //Create a hold presenter
            HoldPresenter holdPresenter = new HoldPresenter(this);
            //fire event to request a list of all assets
            this.Assets_LoadAll.Invoke(this, new EventArgs());
            //fire event to request a list of all groups
            this.Students_LoadAll.Invoke(this, new EventArgs());
            //fire event to load existing holds
            this.Holds_LoadAll.Invoke(this, new EventArgs());
        }

        //==================================================
        // IAssetView Methods
        //==================================================
        public void SetAssetList(List<Asset> assets) {
            this.assetList.Clear();
            foreach (Asset asset in assets) {
                this.assetList.Add(asset.Id, asset);
            }
            RefreshAssetDisplay();
        }
        //==================================================

        //==================================================
        // IStudentView Methods
        //==================================================
        public void SetStudentList(List<Student> students) {
            this.studentList.Clear();
            foreach (Student student in students) {
                studentList.Add(student.StudentId, student);
            }
            RefreshStudentDisplay();
        }
        //==================================================

        //==================================================
        // IHoldView Methods
        //==================================================
        public void SetHoldList(List<Hold> holds) {
            holdList = holds;
        }
        //==================================================

        private void RefreshStudentDisplay(){
            cmbStudentId.Items.Clear();
            foreach (KeyValuePair<string, Student> student in studentList) {
                cmbStudentId.Items.Add(student.Key);
            }
        }

        private void RefreshAssetDisplay() {
            cmbAssetId.Items.Clear();
            foreach (KeyValuePair<string, Asset> asset in assetList) {
                cmbAssetId.Items.Add(asset.Key);
            }
        }

        private void btnSave_Click(object sender, EventArgs e) {
            string assetId = this.cmbAssetId.Items[this.cmbAssetId.SelectedIndex].ToString();
            string studentId = this.cmbStudentId.Items[this.cmbStudentId.SelectedIndex].ToString();

            foreach (Hold hold in holdList) {
                if (hold.AssetId == assetId && hold.StudentId == studentId) {
                    MessageBox.Show("This Asset is already on hold by the selected Student");
                    return;
                }
            }
            try {
                Hold_Create.Invoke(this, new HoldEventArgs(new Hold(studentId, assetId)));
                this.Close();
            } catch (Exception saveException) {
                MessageBox.Show("Error: " + saveException.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }
        //==================================================

    }
}
